package com.qin.malldiscount.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.domain.SmsFlashPromotionSession;
import com.qin.malldiscount.dto.SmsFlashPromotionSessionDetail;
import com.qin.malldiscount.service.SmsFlashPromotionProductRelationService;
import com.qin.malldiscount.service.SmsFlashPromotionSessionService;
import com.qin.malldiscount.mapper.SmsFlashPromotionSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
* @author 覃剑
* @description 针对表【sms_flash_promotion_session(限时购场次表)】的数据库操作Service实现
* @createDate 2022-11-23 19:34:44
*/
@Service
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession>
    implements SmsFlashPromotionSessionService{

    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private SmsFlashPromotionProductRelationService relationService;
    
    /**
     * 添加场次
     */
    @Override
    public int create(SmsFlashPromotionSession promotionSession) {
        promotionSession.setCreateTime(new Date());
        int count = promotionSessionMapper.insert(promotionSession);
        return count;
    }

    /**
     * 修改场次
     */
    @Override
    public int update(Long id, SmsFlashPromotionSession promotionSession) {
        promotionSession.setId(id);
        int count = promotionSessionMapper.updateById(promotionSession);
        return count;
    }

    /**
     * 修改场次启用状态
     */
    @Override
    public int updateStatus(Long id, Integer status) {
        SmsFlashPromotionSession promotionSession = new SmsFlashPromotionSession();
//        promotionSession.setId(id);
        promotionSession.setStatus(status);
        QueryWrapper<SmsFlashPromotionSession> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int count = promotionSessionMapper.update(promotionSession, wrapper);
        return count;
    }

    /**
     * 删除场次
     */
    @Override
    public int delete(Long id) {
        int count = promotionSessionMapper.deleteById(id);
        return count;
    }

    /**
     * 获取详情
     */
    @Override
    public SmsFlashPromotionSession getItem(Long id) {
        SmsFlashPromotionSession promotionSession = promotionSessionMapper.selectById(id);
        return promotionSession;
    }

    /**
     * 根据启用状态获取场次列表
     */
    @Override
    public List<SmsFlashPromotionSession> getList() {
        List<SmsFlashPromotionSession> selectList = promotionSessionMapper.selectList(null);
        return selectList;
    }

    /**
     * 获取全部可选场次及其数量
     */
    @Override
    public List<SmsFlashPromotionSessionDetail> selectList(Long flashPromotionId) {
        List<SmsFlashPromotionSessionDetail> result = new ArrayList<>();
        QueryWrapper<SmsFlashPromotionSession> wrapper = new QueryWrapper<>();
        //1表示启用，0 表示不启用
        wrapper.eq("status",1);
        List<SmsFlashPromotionSession> list = promotionSessionMapper.selectList(wrapper);
        for (SmsFlashPromotionSession promotionSession : list){
            SmsFlashPromotionSessionDetail detail = new SmsFlashPromotionSessionDetail();
            BeanUtil.copyProperties(promotionSession,detail);
            //根据活动和场次id获取商品关系数量
            Long count = relationService.getCount(flashPromotionId, promotionSession.getId());
            detail.setProductCount(count);
            result.add(detail);
        }
        return result;
    }
}




